{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# An introduction to solving biological problems with Python\n",
    "\n",
    "## Day 1 - Session 1: \n",
    "\n",
    "- [Printing values](#Printing-values)\n",
    "- [Using variables](#Using-variables)\n",
    "- [Simple data types](#Simple-data-types): [Booleans](#Booleans), [Integers](#Integers), [Floating point numbers](#Floating-point-numbers), and [Strings](#Strings)\n",
    "- [Comments](#Comments)\n",
    "- [Exercises 1.1.1](#Exercises-1.1.1)\n",
    "- [Arithmetic](#Arithmetic)\n",
    "- [Exercises 1.1.2](#Exercises-1.1.2)\n",
    "- [Saving code in files](#Saving-code-in-files)\n",
    "- [Exercises 1.1.3](#Exercises-1.1.3)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Printing values\n",
    "\n",
    "The first bit of python syntax we're going to learn is the <tt>print</tt> statement. This command lets us print messages to the user, and also to see what Python thinks is the value of some expression (very useful when debugging your programs).\n",
    "\n",
    "We will go into details later on, but for now just note that to print some text you have to enclose it in  \"quotation marks\". \n",
    "\n",
    "We will go into detail on the arithmetic operations supported in python shortly, but you can try exploring python's calculating abilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"Hello from python!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(34)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(2 + 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "You can print  multiple expressions you need to seperate them with commas. Python will insert a space between each element, and a newline at the end of the message (though you can suppress this behaviour by leaving a trailing comma at the end of the command)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "fragment"
    }
   },
   "outputs": [],
   "source": [
    "print(\"The answer:\", 42)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Using variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the <tt>print</tt> commands above we have directly operated on values such as text strings and numbers. When programming we will typically want to deal with rather more complex expressions where it is useful to be able to assign a name to an expression, especially if we are trying to deal with multiple values at the same time.\n",
    "\n",
    "We can give a name to a value using _variables_, the name is apt because the values stored in a variable can _vary_. Unlike some other languages, the type of value assigned to a variable can also change (this is one of the reasons why python is known as a _dynamic_ language).\n",
    "\n",
    "A variable can be assigned to a simple value..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 3\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "... or the outcome of a more complex expression."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 2 + 2\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A variable can be called whatever you like (as long as it starts with a character, it does not contain space and is meaningful) and you assign a value to a variable with the **`=` operator**. Note that this is different to mathematical equality (which we will come to later...)\n",
    "\n",
    "You can <tt>print</tt> a variable to see what python thinks its current value is."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "serine = \"TCA\"\n",
    "print(serine, \"codes for serine\")\n",
    "serine = \"TCG\"\n",
    "print(\"as does\", serine)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the interactive interpreter you don't have to <tt>print</tt> everything, if you type a variable name (or just a value), the interpreter will automatically print out what python thinks the value is. Note though that this is not the case if your code is in a file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "3 + 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 5\n",
    "3 * x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variables can be used on the right hand side of an assignment as well, in which case they will be evaluated before the value is assigned to the variable on the left hand side."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 5\n",
    "y = x * 3\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "or just `y` in the interpreter and in Jupyter notebook"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use the current value of a variable itself in an assignment"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y = y + 1\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In fact this is such a common idiom that there are special operators that will do this implicitly (more on these later)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "y += 1\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simple data types"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python (and computers in general) treats different types of data differently. Python has 5 main basic data types. Types are useful to constrain some operations to a certain category of variables. For example it doesn't really make sense to try to divide a string.\n",
    "\n",
    "We will see some examples of these in use shortly, but for now let's see all of the basic types available in python.\n",
    "\n",
    "### Booleans\n",
    "\n",
    "Boolean values represent truth or falsehood, as used in logical operations, for example. Not surprisingly, there are only two values, and in Python they are called <tt>True</tt> and <tt>False</tt>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = True\n",
    "b = False\n",
    "print(a, b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Integers\n",
    "\n",
    "Integers represent whole numbers, as you would use when counting items, and can be positive or negative."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "i = -7\n",
    "j = 123\n",
    "print(i, j)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Floating point numbers\n",
    "\n",
    "Floating point numbers, often simply referred to as <tt>float</tt>s, are numbers expressed in the decimal system, i.e. 2.1, 999.998, -0.000004 etc. The value 2.0 would also be interpreted as a floating point number, but the value 2, without the decimal point will not; it will be interpreted as an integer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 3.14159\n",
    "y = -42.3\n",
    "print(x * y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Floating point numbers can also carry an <tt>e</tt> suffix that states which power of ten they operate at."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "k = 1.5e3\n",
    "l = 3e-2\n",
    "print(k)\n",
    "print(l)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Strings\n",
    "\n",
    "Strings represent text, i.e. \"strings\" of characters. They can be delimited by single quotes <tt>‘</tt> or double quotes <tt>“</tt>, but you have to use the same delimiter at both ends. Unlike some programming languages, such as Perl, there is no difference between the two types of quote, although using one type does allow the other type to appear inside the string as a regular character.\n",
    "\n",
    "Normally a python statement ends at the end of the line, but if you want to type a string over several lines you can enclose it in triple quotation marks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s = \"ATGTCGTCTACAACACT\"\n",
    "t = 'Serine'\n",
    "u = \"It's a string with apostrophes\"\n",
    "v = \"\"\"A string that extends\n",
    "over multiple lines\"\"\"\n",
    "print(v)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The <tt>None</tt> object\n",
    "\n",
    "The None object is special built-in value which can be thought of as **representing nothingness or that something is undefined**. For example, it can be used to indicate that a variable exists, but has not yet been set to anything specific."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "z = None\n",
    "print(z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Object type\n",
    "\n",
    "You can check what type python thinks an expression is with the <tt>type</tt> function, which you can call with the name <tt>type</tt> immediately followed by parentheses enclosing the expression you want to check (either a variable or a value), e.g. <tt>type(3)</tt>. (This is the general form for calling functions, we'll see lots more examples of functions later...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "a = True\n",
    "print(a, \"is of\", type(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "i = -7\n",
    "print(i, \"is of\", type(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 12.7893\n",
    "print(x, \"is of\", type(x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "s = \"ATGTCGTCTACAACACT\"\n",
    "print(s, \"is of\", type(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "z = None\n",
    "print(z, \"is of\", type(z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comments\n",
    "\n",
    "When you are writing a program it is often convenient to annotate your code to remind you what you were (intending) it to do. In programming these annotations are known as _comments_. You can include a comment in python by prefixing some text with a <tt>#</tt> character. All text following the <tt>#</tt> will then be ignored by the interpreter. You can start a comment on its own line, or you can include it at the end of a line of code.\n",
    "\n",
    "It is also often useful to temporarily remove some code from a script without deleting it. This is known as _commenting out_ some code."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(\"Hi\") # this will be ignored\n",
    "# as will this\n",
    "print(\"Bye\")\n",
    "# print \"Never seen\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises 1.1.1\n",
    "\n",
    "To start the Python interpreter, open a terminal window, type the command `python`, then enter Python commands after the prompt `>>>` and press `Enter` when you're done. \n",
    "\n",
    "Python will run the code you typed, and might display some output on the line below, before leaving you with another prompt which looks like `>>>`.\n",
    "\n",
    "If you want to exit the interactive interpreter you can type the command `quit()` or type `Ctrl-D`.\n",
    "\n",
    "In the interpreter:\n",
    "\n",
    "1. Create a variable and assign it the string value of your first name, assign your age to another variable (you are free to lie!), print out a message saying how old you are\n",
    "2. Use the addition operator to add 10 to your age and print out a message saying how old you will be in 10 years time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Arithmetic"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python supports all the standard arithmetical operations on numerical types, and mostly uses a similar syntax to several other computer languages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 4.5\n",
    "y = 2\n",
    "\n",
    "print('x', x, 'y', y)\n",
    "print('addition x + y =', x + y) \n",
    "print('subtraction x - y =', x - y) \n",
    "print('multiplication x * y =', x * y) \n",
    "print('division x / y =', x / y) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 4.5\n",
    "y = 2\n",
    "\n",
    "print('x', x, 'y', y)\n",
    "print('division x / y =', x / y)\n",
    "print('floored division x // y =', x // y) \n",
    "print('modulus (remainder of x/y) x % y =', x % y) \n",
    "print('exponentiation x ** y =', x ** y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As usual in maths, division and multiplication have higher precedence than addition and subtraction, but arithmetic expressions can be grouped using parentheses to override the default precedence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 13\n",
    "y = 5\n",
    "\n",
    "print('x * (2 + y) =', x * (2 + y))\n",
    "print('(x * 2) + y =', (x * 2) + y)\n",
    "print('x * 2 + y =', x * 2 + y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can mix (some) types in arithmetic expressions and python will apply rules as to the type of the result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "13 + 5.0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can force python to use a particular type by converting an expression explicitly, using helpful named functions: <tt>float</tt>, <tt>int</tt>, <tt>str</tt> etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "float(3) + float(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "int(3.14159) + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The addition operator `+` allows you also to concatenate strings together."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('number' + str(3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Division in Python 2 sometimes trips up new (and experienced!) programmers. If you divide 2 integers you will only get an integer result. If you want a floating point result you should explicitly cast at least one of the arguments to a <tt>float</tt>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(\"3/4 =\", 3/4)\n",
    "print(\"3.0/4 =\", 3.0/4)\n",
    "print(\"float(3)/4 =\", float(3)/4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are a few shortcut assignment statements to make modifying variables directly faster to type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 3\n",
    "x += 1 # equivalent to x = x + 1\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "x = 2\n",
    "y = 10\n",
    "y *= x\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These shortcut operators are available for all arithmetic and logical operators."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises 1.1.2\n",
    "\n",
    "In the interpreter:\n",
    "\n",
    "1. Assign numerical values to 2 variables, calculate the mean of these two variables and store the result in another variable. Print out the result to the screen."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Saving code in files\n",
    "\n",
    "### Excecute code in files\n",
    "\n",
    "As we mentioned earlier, you can also save python code in a file and then execute it later. We typically save python code in a file ending with the extension <tt>.py</tt>. The file, or _script_, can then be executed simply by supplying the name of the file as an argument to the <tt>python</tt> command in the terminal.\n",
    "\n",
    "The first file we will be looking at is located in the `scripts` directory and it is called `hello.py`. To execute the script, open a terminal window, navigate to the `scripts` directory and execute the code in the script `hello.py` by running `python hello.py` in your terminal:\n",
    "\n",
    "```bash\n",
    "ls\n",
    "cd scripts\n",
    "python hello.py\n",
    "```\n",
    "\n",
    "Shell commands:\n",
    "- `ls`: to list directory contents\n",
    "- `pwd`: to return working directory name\n",
    "- `cd to/this/directory/`: to change directory\n",
    "\n",
    "### Edit code in files\n",
    "\n",
    "You can use any text editor you know to edit your file, but the file should be saved as plain text, so programs like Microsoft Word aren't the best choice. Many text editors will highlight python syntax for you which can help avoid syntax errors.\n",
    "\n",
    "To open any Python scripts in a text editor, open [Gedit](https://wiki.gnome.org/Apps/Gedit) or [Atom](https://atom.io/) and use the File menu, navigate to the `scripts` directory and open `hello.py`. \n",
    "\n",
    "You can now modify the `print` statement, save the file and go back to the terminal window you've just opened to run the code again by using the command `python hello.py`.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Exercises 1.1.3\n",
    "\n",
    "Create a new Python file to solve these exercises. It is good practice to create a new file each time you solve a new problem.\n",
    "\n",
    "1. Look up the <a href=\"http://en.wikipedia.org/wiki/DNA_codon_table\">genetic code</a>. Create four string variables that store possible DNA encodings of serine (S), leucine (L), tyrosine (Y) and cysteine (C). Where multiple codings are available, just pick one for now.\n",
    "2. Create a variable containing a possible DNA sequence for the protein sequence SYLYC. (Note that the addition operator <tt>+</tt> allows you to concatenate strings together.) Print the DNA sequence.\n",
    "3. Include a comment in your file to remind you the purpose of the script"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Next session\n",
    "\n",
    "Go to our next notebook: [Introduction_to_python_day_1_session_2](Introduction_to_python_day_1_session_2.ipynb)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
